<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
 <el-form-item label="类型" prop="isXt">
        <el-select placeholder="请选择类型" v-model="queryParams.isXt" @keyup.enter.native="handleQuery">
          <el-option label="销售单" value="3" />
          <el-option label="销退单" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="saleList" >
      <el-table-column type="index" align="center" label="序号" />
      <el-table-column label="单号" align="center" prop="saleCode" />
      <el-table-column label="客户" align="center" prop="supplyName" />
      <el-table-column label="总数量" align="center" prop="znum" />
      <el-table-column label="总金额" align="center" prop="zprice" />
     <el-table-column label="类型" align="center" prop="isXt">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isXt === '3'">销售单</el-tag>
          <el-tag  v-if="scope.row.isXt === '4'">销退单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="审核状态" align="center" prop="isSh" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isSh === '0'">已通过</el-tag>
          <el-tag type="info" v-if="scope.row.isSh === '1'">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.isSh === '2'">未通过</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSh(scope.row)"
            v-hasPermi="['supermarket:xqsale:list']"
          >查看</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleZf(scope.row)"
            v-hasPermi="['supermarket:sale:zfSale']"
          >作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-table show-summary v-loading="loading" :data="xqsaleList"     :summary-method="getSummaries">
      <el-table-column type="index" align="center" label="序号" />
      <el-table-column label="商品码" align="center" prop="productcode" />
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="规格" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="sum" />
        <el-table-column label="价格" align="center" prop="price" />
        <el-table-column label="总金额" align="center" prop="zprice" />
        <el-table-column
          label="生产日期"
          align="center"
          prop="manufactureDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.manufactureDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="过期日期"
          align="center"
          prop="expirationDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.expirationDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center" prop="productBrand" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listSaleZf,zfSale,listSale, getSale, delSale, addSale, updateSale, auditSale} from "@/api/supermarket/sale";
import { listXqsale, getXqsale, delXqsale, addXqsale, updateXqsale } from "@/api/supermarket/xqsale";
export default {
  name: "Sale",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售汇总表格数据
      saleList: [],
      xqsaleList:[],
      // 弹出层标题
      title: "",
      saleId: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleCode: null,
        supplyName: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        isXt: null,
        isZf: null,
        isRk: null,
        isSh: 1,
        shName: null,
        shTime: null,
        shRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售汇总列表 */
    getList() {
      this.loading = true;
      listSaleZf(this.queryParams).then(response => {
        this.saleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 销售汇总详情
    getListSale(row) {
      const data = {
        saleId: row.saleId
      }
      listXqsale(data).then(response => {
        this.xqsaleList = response.data;
      });
    },

    // 审核弹框
    handleSh(row) {
       this.title =
        "详情：" +
        row.saleCode +
        ">创建人:" +
        row.createName +
        ">供应商:" +
        row.supplyName;
        this.getListSale(row);
        this.saleId = row.saleId
        this.open = true;
    },
    handleZf(row) {
      zfSale(row.saleId).then(res => {
         this.$modal.msgSuccess("作废通过！");
        this.getList();
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        saleId: null,
        saleCode: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        createTime: null,
        isXt: null,
        isZf: null,
        isRk: null,
        isSh: null,
        shName: null,
        shTime: null,
        shRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
        getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index === 5) {
          sums[index] = "";
          return;
        }
        if (index === 9) {
          sums[index] = "";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
  }
};
</script>
